/*
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.Maps;

import java.util.ArrayList;
import java.util.List;

import android.location.Location;
import android.os.Bundle;

import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.Api.LibreGeoSocial;
import com.LibreGeoSocial.Api.User;
import com.LibreGeoSocial.App.DataManager;
import com.LibreGeoSocial.App.R;
import com.LibreGeoSocial.App.Overlay.GenericOverlay;
import com.LibreGeoSocial.App.Overlay.PositionOverlay;
import com.LibreGeoSocial.App.Routes.LocationService;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class ResourcesMap extends MapActivity{
	
	private MapView map;
	private User user;
	private Location currentLocation = null;
	
	private ArrayList<GeoNode> resources_list = null;
	
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        user = LibreGeoSocial.getInstance().getInfoUser(true, null);
        currentLocation = LocationService.getCurrentLocation();
        
        resources_list = cleanNoLocation((ArrayList<GeoNode>) DataManager.getInstance().getData(DataManager.RESOURCES_LIST));
        init();
        
    }

	public void init(){
		
		map = new MapView(this, getString(R.string.map_key));
		setContentView(map);

		//Adding a zoom control
		map.setBuiltInZoomControls(true);
		
		map.setClickable(true);
		map.setSatellite(true);
		map.setStreetView(false);
		
		addResourceOverlay();
		if(currentLocation != null){
			setPositionInMap(17);
		}
	}
    
	private void setPositionInMap(int zoom){
		
		GeoPoint point = new GeoPoint(  (int) (currentLocation.getLatitude() * 1E6), 
				  (int) (currentLocation.getLongitude() * 1E6));
		
		map.getController().animateTo(point);
		map.getController().setZoom(zoom);
		
		PositionOverlay myposOverlay = new PositionOverlay();
        myposOverlay.setUser(user);
        
	    final List<Overlay> overlays = map.getOverlays();
	    overlays.add( myposOverlay );
	}
	
	private void addResourceOverlay(){
		if(resources_list == null)
			return;
		
	    final List<Overlay> overlays = map.getOverlays();

		int max = resources_list.size();
		for (int i = 0; i < max; i++){
			GenericOverlay overlay = new GenericOverlay(this, resources_list.get(i));
			overlays.add( overlay );
		}
	}
	
	private ArrayList<GeoNode> cleanNoLocation(ArrayList<GeoNode> original_list){
		ArrayList<GeoNode> list = new ArrayList<GeoNode>();

		int max = original_list.size();
		for(int i = 0; i < max; i++){
			GeoNode node = original_list.get(i);
			if((node.getLatitude() == -1) && (node.getLongitude() == -1))
				continue;
			list.add(node);
		}
		return list;
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
}